/*
 * DownloadOPDSView.java
 */

package main;

import helper.ParseOpds;
import downloadopds.model.CatalogueOpds;
import java.awt.Toolkit;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 * The application's main frame.
 */
public class DownloadOPDSView extends FrameView {

    private ParseOpds swingWorker;

    public DownloadOPDSView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        progressBar.setVisible(true);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    progressBar.setVisible(true);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DownloadOPDSApp.getApplication().getMainFrame();
            aboutBox = new DownloadOPDSAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
            aboutBox.setTitle("A propos");
        }
        DownloadOPDSApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        split = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        adrDropBox = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        downloadButton = new javax.swing.JButton();
        adrCollecion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dirEbooks = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        stopButton = new javax.swing.JButton();
        parcourirButton = new javax.swing.JButton();
        collectionButton = new javax.swing.JRadioButton();
        categorieButton = new javax.swing.JRadioButton();
        adrCategorie = new javax.swing.JTextField();
        catalogueButton = new javax.swing.JRadioButton();
        aideDropBox = new javax.swing.JButton();
        aideCollection = new javax.swing.JButton();
        aideCategorie = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cbFormat = new javax.swing.JComboBox();
        repCatalogue = new javax.swing.JButton();
        logScroll = new javax.swing.JScrollPane();
        logArea = new javax.swing.JTextPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        dlButtonGroup = new javax.swing.ButtonGroup();

        mainPanel.setName("mainPanel"); // NOI18N

        split.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        split.setName("split"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(DownloadOPDSView.class);
        adrDropBox.setFont(resourceMap.getFont("adrDropBox.font")); // NOI18N
        adrDropBox.setText(resourceMap.getString("adrDropBox.text")); // NOI18N
        adrDropBox.setToolTipText(resourceMap.getString("adrDropBox.toolTipText")); // NOI18N
        adrDropBox.setName("adrDropBox"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        downloadButton.setIcon(resourceMap.getIcon("downloadButton.icon")); // NOI18N
        downloadButton.setText(resourceMap.getString("downloadButton.text")); // NOI18N
        downloadButton.setName("downloadButton"); // NOI18N
        downloadButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                downloadButtonMouseClicked(evt);
            }
        });

        adrCollecion.setText(resourceMap.getString("adrCollecion.text")); // NOI18N
        adrCollecion.setToolTipText(resourceMap.getString("adrCollecion.toolTipText")); // NOI18N
        adrCollecion.setName("adrCollecion"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        dirEbooks.setFont(resourceMap.getFont("dirEbooks.font")); // NOI18N
        dirEbooks.setText(resourceMap.getString("dirEbooks.text")); // NOI18N
        dirEbooks.setEnabled(false);
        dirEbooks.setName("dirEbooks"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        stopButton.setIcon(resourceMap.getIcon("stopButton.icon")); // NOI18N
        stopButton.setText(resourceMap.getString("stopButton.text")); // NOI18N
        stopButton.setEnabled(false);
        stopButton.setName("stopButton"); // NOI18N
        stopButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                stopButtonMouseClicked(evt);
            }
        });

        parcourirButton.setText(resourceMap.getString("parcourirButton.text")); // NOI18N
        parcourirButton.setName("parcourirButton"); // NOI18N
        parcourirButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                parcourirButtonMouseClicked(evt);
            }
        });

        dlButtonGroup.add(collectionButton);
        collectionButton.setText(resourceMap.getString("collectionButton.text")); // NOI18N
        collectionButton.setName("collectionButton"); // NOI18N

        dlButtonGroup.add(categorieButton);
        categorieButton.setText(resourceMap.getString("categorieButton.text")); // NOI18N
        categorieButton.setName("categorieButton"); // NOI18N

        adrCategorie.setToolTipText(resourceMap.getString("adrCategorie.toolTipText")); // NOI18N
        adrCategorie.setName("adrCategorie"); // NOI18N

        dlButtonGroup.add(catalogueButton);
        catalogueButton.setSelected(true);
        catalogueButton.setText(resourceMap.getString("catalogueButton.text")); // NOI18N
        catalogueButton.setName("catalogueButton"); // NOI18N

        aideDropBox.setIcon(resourceMap.getIcon("aideDropbox.icon")); // NOI18N
        aideDropBox.setText(resourceMap.getString("aideDropbox.text")); // NOI18N
        aideDropBox.setBorder(null);
        aideDropBox.setBorderPainted(false);
        aideDropBox.setContentAreaFilled(false);
        aideDropBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        aideDropBox.setName("aideDropbox"); // NOI18N
        aideDropBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aideDropBoxMouseClicked(evt);
            }
        });

        aideCollection.setIcon(resourceMap.getIcon("aideCollection.icon")); // NOI18N
        aideCollection.setBorderPainted(false);
        aideCollection.setContentAreaFilled(false);
        aideCollection.setMargin(new java.awt.Insets(0, 0, 0, 0));
        aideCollection.setName("aideCollection"); // NOI18N
        aideCollection.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aideCollectionMouseClicked(evt);
            }
        });

        aideCategorie.setIcon(resourceMap.getIcon("aideCategorie.icon")); // NOI18N
        aideCategorie.setBorderPainted(false);
        aideCategorie.setContentAreaFilled(false);
        aideCategorie.setMargin(new java.awt.Insets(0, 0, 0, 0));
        aideCategorie.setName("aideCategorie"); // NOI18N
        aideCategorie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aideCategorieMouseClicked(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        cbFormat.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Epub", "Mobi", "PDF" }));
        cbFormat.setName("cbFormat"); // NOI18N

        repCatalogue.setIcon(resourceMap.getIcon("repCatalogue.icon")); // NOI18N
        repCatalogue.setBorder(null);
        repCatalogue.setBorderPainted(false);
        repCatalogue.setContentAreaFilled(false);
        repCatalogue.setMargin(new java.awt.Insets(0, 0, 0, 0));
        repCatalogue.setName("repCatalogue"); // NOI18N
        repCatalogue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                repCatalogueMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                .add(downloadButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(stopButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(4, 4, 4))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel3)
                                    .add(jLabel2))
                                .add(18, 18, 18)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(catalogueButton)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .add(dirEbooks, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                                        .add(18, 18, 18)
                                        .add(parcourirButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(cbFormat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 195, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .add(adrDropBox, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(repCatalogue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(jPanel1Layout.createSequentialGroup()
                                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(collectionButton)
                                            .add(categorieButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(org.jdesktop.layout.GroupLayout.TRAILING, adrCategorie, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                                            .add(adrCollecion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)))))))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(164, 164, 164)
                        .add(jLabel1))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel5)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(aideCategorie, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(aideCollection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(aideDropBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(78, 78, 78))
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel4)
                .addContainerGap(780, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(17, 17, 17)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(aideDropBox)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(1, 1, 1)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(adrDropBox))
                        .add(23, 23, 23)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(catalogueButton)
                            .add(jLabel3)))
                    .add(repCatalogue))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(adrCollecion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(aideCollection, 0, 0, Short.MAX_VALUE)
                        .add(collectionButton)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(adrCategorie, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(aideCategorie, 0, 0, Short.MAX_VALUE)
                        .add(categorieButton)))
                .add(24, 24, 24)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(cbFormat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(dirEbooks, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(parcourirButton))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(downloadButton)
                    .add(stopButton))
                .addContainerGap())
        );

        split.setTopComponent(jPanel1);

        logScroll.setName("logScroll"); // NOI18N

        logArea.setName("logArea"); // NOI18N
        logScroll.setViewportView(logArea);

        split.setBottomComponent(logScroll);

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, split, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(split, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setLabel(resourceMap.getString("fileMenu.label")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(DownloadOPDSView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setLabel(resourceMap.getString("exitMenuItem.label")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setLabel(resourceMap.getString("helpMenu.label")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setLabel(resourceMap.getString("aboutMenuItem.label")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N
        progressBar.setOpaque(true);

        org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
            .add(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(statusMessageLabel)
                .addContainerGap(855, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(705, Short.MAX_VALUE)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(14, 14, 14))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusMessageLabel)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(16, 16, 16))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void downloadButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_downloadButtonMouseClicked
        int typePage = CatalogueOpds.CATALOGUE;
        String numeroTypePage = null;
        CatalogueOpds catalogue;

        //Si un des champs texte est vide, affichage message erreur
        if(adrDropBox.getText().isEmpty() || dirEbooks.getText().isEmpty()) {
            String message = "L'index de la dropbox et le dossier de sortie sont obligatoires!";
            JOptionPane.showMessageDialog(new JFrame(), message, "Erreur",
            JOptionPane.ERROR_MESSAGE);
            return;
        }

        //Suivant ce qu'on décide de télécharger on vérifie les contenus
        if(categorieButton.isSelected()){
            if(adrCategorie.getText().isEmpty()){
                String message = "L'adresse de la catégorie doit être renseignée!";
                JOptionPane.showMessageDialog(new JFrame(), message, "Erreur",
                JOptionPane.ERROR_MESSAGE);
                return;
            }
            typePage = CatalogueOpds.CATEGORIE;
            numeroTypePage = adrCategorie.getText();
        }
        if(collectionButton.isSelected()){
            if(adrCollecion.getText().isEmpty()){
                String message = "L'adresse de la collection doit être renseignée!";
                JOptionPane.showMessageDialog(new JFrame(), message, "Erreur",
                JOptionPane.ERROR_MESSAGE);
                return;
            }
            typePage = CatalogueOpds.COLLECTION;
            numeroTypePage = adrCollecion.getText();
        }

        //Réinitialisation de la zone de log
        this.logArea.setText("");

        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        statusMessageLabel.setText("Téléchargement...");

        //Création du catalogue
        catalogue = new CatalogueOpds(adrDropBox.getText(), typePage, numeroTypePage, dirEbooks.getText());
        swingWorker = new ParseOpds(this, catalogue);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {              
                //Lancement du thread en parallèle
                swingWorker.execute();
            }
        });
    }//GEN-LAST:event_downloadButtonMouseClicked

    private void stopButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopButtonMouseClicked
        swingWorker.cancel(true);
    }//GEN-LAST:event_stopButtonMouseClicked

    private void parcourirButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parcourirButtonMouseClicked
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setApproveButtonText("Choix du dossier...");
        int retour = chooser.showOpenDialog(new JFrame());
        if (retour == JFileChooser.APPROVE_OPTION) {
            dirEbooks.setText(chooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_parcourirButtonMouseClicked

    private void aideDropBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aideDropBoxMouseClicked
        String message = "Index du catalogue OPDS. Cela correspod à sa page d'accueil.\nEx :\n\t- http://dl.dropbox.com/u/xxxxxxx/catalogue/index.html\n\t- https://dl.dropbox.com/u/xxxxxxx/Ebooks/catalogue/index.html";
        JOptionPane.showMessageDialog(new JFrame(), message, "Aide",
        JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_aideDropBoxMouseClicked

    private void aideCollectionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aideCollectionMouseClicked
        String message = "Le numéro de la collection.\nIl faut se rendre sur la page web de la collection et noter le numéro.\n Ex : http://dl.dropbox.com/u/xxxxxxx/catalogue/serie/serie_96.html\n On note le numéro 96.";
        JOptionPane.showMessageDialog(new JFrame(), message, "Aide",
        JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_aideCollectionMouseClicked

    private void aideCategorieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aideCategorieMouseClicked
        String message = "Le numéro de la catégorie.\nIl faut se rendre sur la page web de la catégorie et noter le numéro.\n Ex : http://dl.dropbox.com/u/xxxxxxx/catalogue/tag/tag_41.html\n On note le numéro 41.";
        JOptionPane.showMessageDialog(new JFrame(), message, "Aide",
        JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_aideCategorieMouseClicked

    private void repCatalogueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_repCatalogueMouseClicked
        //Ouverture de la collection de catalogues
        if (choixCatalogue == null) {
            JFrame mainFrame = DownloadOPDSApp.getApplication().getMainFrame();
            choixCatalogue = new ChoixCatalogue(mainFrame, true, this);
            choixCatalogue.setLocationRelativeTo(mainFrame);
        }
        DownloadOPDSApp.getApplication().show(choixCatalogue);
    }//GEN-LAST:event_repCatalogueMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JTextField adrCategorie;
    public javax.swing.JTextField adrCollecion;
    public javax.swing.JTextField adrDropBox;
    public javax.swing.JButton aideCategorie;
    public javax.swing.JButton aideCollection;
    public javax.swing.JButton aideDropBox;
    public javax.swing.JRadioButton catalogueButton;
    public javax.swing.JRadioButton categorieButton;
    public javax.swing.JComboBox cbFormat;
    public javax.swing.JRadioButton collectionButton;
    public javax.swing.JTextField dirEbooks;
    public javax.swing.ButtonGroup dlButtonGroup;
    public javax.swing.JButton downloadButton;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel jLabel5;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JTextPane logArea;
    public javax.swing.JScrollPane logScroll;
    public javax.swing.JPanel mainPanel;
    public javax.swing.JMenuBar menuBar;
    public javax.swing.JButton parcourirButton;
    public javax.swing.JProgressBar progressBar;
    public javax.swing.JButton repCatalogue;
    public javax.swing.JSplitPane split;
    public javax.swing.JLabel statusMessageLabel;
    public javax.swing.JPanel statusPanel;
    public javax.swing.JButton stopButton;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    private JDialog choixCatalogue;
}
